from common.models import *
from mdeditor.fields import MDTextField


class Flow(models.Model):
    name = models.CharField(verbose_name=u'流程名称', max_length=20)
    is_active = models.NullBooleanField(verbose_name=u'是否激活', default=True, help_text='如果只创建未激活, 用户无法审批')
    remark = models.CharField(verbose_name=u'备注', default='', max_length=200, blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'flow'
        verbose_name = '流程表'
        verbose_name_plural = verbose_name


# 例如上级领导审批/部门经理审批/财务经理审批/商务经理审批/工厂经理审批/总经理审批


class FlowRole(models.Model):
    name = models.CharField(verbose_name=u'角色名称', max_length=30)
    role_type = models.SmallIntegerField(verbose_name=u'角色分类', default=1,
                                         choices=((1, '直接上级'), (2, '部门主管'), (3, '部门经理'), (4, '指定审批人')),
                                         help_text='若指定审批人, 必须添加审批人, 否则默认为直接上级')
    users = models.ManyToManyField(verbose_name=u'指定审批人', to=User, blank=True, help_text=u'添加审批人')
    remark = models.CharField(verbose_name=u'备注', max_length=100, blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'flow_role'
        verbose_name = '流程角色表'
        verbose_name_plural = verbose_name


# 节点可执行操作
# 定义每个节点当事人可执行的操作选项


class OperationOption(models.Model):
    name = models.SmallIntegerField(verbose_name=u'操作名称',
                                    choices=((1, '用户提交'), (2, '同意'), (3, '撤回/收回'), (4, '驳回/退回')),
                                    unique=True, blank=True, null=True, default=1)
    remark = models.CharField(verbose_name=u'备注', max_length=100, blank=True, default='')

    def __str__(self):
        return self.get_name_display()

    class Meta:
        db_table = 'flow_operation_options'
        verbose_name = '流程操作表'
        verbose_name_plural = verbose_name


class FlowNode(models.Model):
    flow = models.ForeignKey(verbose_name=u'节点所属流程', to=Flow)
    flow_node_name = models.CharField(verbose_name=u'流程节点名称', max_length=40)
    #  通过node_type来寻找开始节点和结束节点
    node_type = models.SmallIntegerField(verbose_name=u'节点类型',
                                         choices=((1, '开始节点'), (2, '子节点'), (3, '结束')))
    order = models.SmallIntegerField(verbose_name=u'节点执行顺序', blank=True, null=True)
    pre_node = models.ForeignKey(verbose_name=u'节点的前一个节点', to='FlowNode', blank=True, null=True,
                                 help_text=u'如果为空,节点类型变为结束节点,流程结束', related_name='flow_pre_node')
    next_node = models.ForeignKey(verbose_name=u'节点的下一个节点', to='FlowNode', blank=True, null=True,
                                  help_text=u'如果为空,节点类型变为结束节点,流程结束', related_name='flow_next_node')
    flow_role = models.ForeignKey(verbose_name=u'节点操作角色', to='FlowRole')
    flow_operation = models.ManyToManyField(verbose_name=u'节点可操作选项', to='OperationOption')
    remark = models.CharField(verbose_name=u'备注', max_length=100, blank=True, null=True)

    def __str__(self):
        return '%s:%s' % (self.flow.name, self.flow_node_name)

    class Meta:
        db_table = 'flow_node'
        verbose_name = '流程节点表'
        verbose_name_plural = verbose_name


class Suggest(models.Model):
    id = models.AutoField(verbose_name=u'建议ID', primary_key=True)
    title = models.CharField(verbose_name=u'主题', max_length=100)
    #  小组作为一个小部门看待
    department = models.ForeignKey(verbose_name=u'部门', to=Department, blank=True, null=True)
    create_user = models.ForeignKey(verbose_name=u'创建人', to=User, related_name='create_user')
    create_at = models.DateTimeField(verbose_name=u'创建时间', auto_now_add=True)
    update_at = models.DateTimeField(verbose_name=u'最后修改时间', auto_now=True)
    # 是否采纳,需要所有审批人同意后,被采纳
    state = models.SmallIntegerField(verbose_name=u'建议状态',
                                     choices=((1, '已提交'), (2, '审批中'), (3, '审批结束'), (4, '用户撤回'), (5, '被驳回')), default=2,
                                     blank=True, null=True)
    current_node = models.ForeignKey(verbose_name=u'当前节点', to=FlowNode, default=1)
    current_situation = MDTextField(verbose_name=u'当前的状况')
    proposed_solution = MDTextField(verbose_name=u'解决方案')
    benefit = MDTextField(verbose_name=u'预计有哪些收益')
    resource = MDTextField(verbose_name=u'需要那些资源', default='')

    def __str__(self):
        return self.title

    class Meta:
        db_table = 'flow_suggest'
        verbose_name = '合理化建议表'
        verbose_name_plural = verbose_name


# 审批意见
class SuggestAudit(models.Model):
    operation = models.ForeignKey(verbose_name=u'审批操作', to=OperationOption, blank=True, null=True, default=None)
    audit_user = models.ForeignKey(verbose_name=u'审批人', to=User, blank=True, null=True, default=None)
    audit_time = models.DateTimeField(verbose_name=u'审批时间', auto_now_add=True)
    audit_suggest = models.CharField(verbose_name=u'审批意见', default='', blank=True, null=True, max_length=200)
    # 审批意见对应的表单,不能为空,每个审批记录都对应一个表单和一个节点
    suggest = models.ForeignKey(verbose_name=u'对应的表单', to=Suggest)
    flow_node = models.ForeignKey(verbose_name=u'对应流程节点', to='FlowNode', blank=True, null=True)

    def __str__(self):
        return '%s:%s' % (self.operation.name, self.audit_suggest)

    class Meta:
        db_table = 'flow_suggest_audit'
        verbose_name = '合理化建议流程审批表'
        verbose_name_plural = verbose_name


# 项目
class Project(models.Model):
    suggest = models.OneToOneField(verbose_name=u'对应建议', to=Suggest)
    in_charge = models.ForeignKey(verbose_name=u'负责人', to=User, related_name='in_charge_user')
    create_user = models.ForeignKey(verbose_name=u'创建人', to=User, related_name='in_create_user')
    verify_user = models.ForeignKey(verbose_name=u'验证人', to=User, blank=True, null=True, related_name='verify_user')
    benefit_money = models.SmallIntegerField(verbose_name=u'项目收益(金钱)',default=0)
    estimate_at = models.DateTimeField(verbose_name=u'预计完成日期', blank=True, null=True)
    finish_at = models.DateTimeField(verbose_name=u'实际完成时间', blank=True, null=True)
    create_at = models.DateTimeField(verbose_name=u'项目创建时间', auto_now_add=True)
    update_at = models.DateTimeField(verbose_name=u'项目更新时间', auto_now=True)
    content = MDTextField(verbose_name=u'项目内容', default='')
    verify_result = MDTextField(verbose_name=u'验证结果', default='')
    def __str__(self):
        return self.suggest.title

    class Meta:
        db_table = 'suggest_project'
        verbose_name = '项目表'
        verbose_name_plural = verbose_name


class ProjectRecord(models.Model):
    project = models.ForeignKey(verbose_name=u'对应项目', to=Project)
    content = MDTextField(verbose_name=u'记录内容')
    create_user = models.ForeignKey(verbose_name=u'记录人', to=User)
    create_at = models.DateTimeField(verbose_name=u'更新时间', auto_now_add=True)

    def __str__(self):
        return self.project.suggest.title

    class Meta:
        db_table = 'suggest_project_record'
        verbose_name = '项目跟进记录'
        verbose_name_plural = verbose_name
